package com.edu.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.entity.Trun;
import com.edu.entity.User;
import com.edu.service.TrunService;
import com.edu.service.Impl.TrunServiceImpl;

/**
 * Servlet implementation class TrunServletAll
 */
@WebServlet("/TrunServletAll")
public class TrunServletAll extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TrunServletAll() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("1111111111");
		request.setCharacterEncoding("UTF-8");
		String menu = request.getParameter("menu");
		System.out.println(menu);
		//查询
		if ("1".equals(menu)) {
			queryByUser(request,response);
		//修改
		}else if("2".equals(menu)) {
			userUpdate(request, response);
		//添加
		}else if("3".equals(menu)) {
			userAdd(request, response);
		//删除
		}else if("4".equals(menu)) {
			userDelete(request, response);
		}else {
			System.out.println("sorry!");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	//查询
	protected void queryByUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int TrunId = -1;
		if(!("".equals(request.getParameter("TrunId")))){
			TrunId = Integer.parseInt(request.getParameter("TrunId"));
		}
		String tType = request.getParameter("tType");
		String tNum = request.getParameter("tNum");
		String tCost = request.getParameter("tCost");
		Trun trun = new Trun(TrunId, tType, tNum, tCost);
		TrunService trunServlet = new TrunServiceImpl();
		List<Trun> list = trunServlet.selectByTrun(trun);
		request.setAttribute("list",list);
		//response.sendRedirect("index.jsp");
		request.getRequestDispatcher("Trun.jsp").forward(request, response);
	}
		
	//修改
	protected void userUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int TrunId = Integer.parseInt(request.getParameter("TrunId"));
		String tType = request.getParameter("tType");
		String tNum = request.getParameter("tNum");
		String tCost = request.getParameter("tCost");
		Trun trun = new Trun(TrunId, tType, tNum, tCost);
		TrunService trunServlet = new TrunServiceImpl();
		trunServlet.updateTrun(trun);
		response.sendRedirect("Trun.jsp");
	}
		
	//添加
	protected void userAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String tType = request.getParameter("tType");
		String tNum = request.getParameter("tNum");
		String tCost = request.getParameter("tCost");
		Trun trun = new Trun(tType, tNum, tCost);
		TrunService trunServlet = new TrunServiceImpl();
		trunServlet.addTrun(trun);
		response.sendRedirect("Trun.jsp");
	}
		
		//删除
		protected void userDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			// TODO Auto-generated method stub
			request.setCharacterEncoding("UTF-8");
			String[] ids = request.getParameterValues("selectUid");

			TrunService trunServlet = new TrunServiceImpl();
			trunServlet.deleteTrunAll(ids);
			response.sendRedirect("Trun.jsp");
		}
}
